#!/usr/bin/env bash

# Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

srcdir=Python-${pkgver}
mkdir build || exit 1
cd build || exit 1

extra_args=()
if [ "${arch}" != "${host_arch}" ] || [ ${host_toolchain} -eq 1 ]; then
    extra_args+=(--host=${triple})
    extra_args+=(--build=${host_triple})

    if [ ! -z "${feature_cross}" ]; then
        extra_args+=(--with-build-python=/host/usr/bin/python3)
    else
        extra_args+=(--with-build-python=/usr/bin/python3)
        extra_args+=(--enable-optimizations)
    fi

    if [ -e /dev/ptmx ]; then
        extra_args+=(ac_cv_file__dev_ptmx=yes)
    else
        extra_args+=(ac_cv_file__dev_ptmx=no)
    fi

    if [ -e /dev/ptc ]; then
        extra_args+=(ac_cv_file__dev_ptc=yes)
    else
        extra_args+=(ac_cv_file__dev_ptc=no)
    fi
fi

../${srcdir}/configure \
    --prefix=/usr \
    --enable-shared \
    --disable-ipv6 \
    ${extra_args[@]} \
    || exit 1
make -j ${MAX_JOBS}
